package com.yulongtian.offers.day202209.day20220905;

import java.util.*;

/**
 * https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/
 *
 * @author YulongTian
 * @date 2022/9/5 - 14:25
 */
public class Test04 {
    public static void main(String[] args) {

    }

    public static int[] levelOrder(TreeNode root) {
        if(root==null){
            return new int[]{};
        }
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        List<Integer> res = new ArrayList<>();
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            res.add(poll.val);
            if (poll.left != null) {
                queue.add(poll.left);
            }
            if (poll.right != null) {
                queue.add(poll.right);
            }
        }
        int[] ress = new int[res.size()];
        int j = 0;
        for (Integer re : res) {
            ress[j++] = re;
        }


        return ress;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

}
